package table.node;

import java.util.List;

public class TableNode extends BaseNode {

	private String struct = "table";
	
	public String traveler() {
		String  result = "<div class='TableNode'>";
		int rowSize = 0;
		
		DataNode firstColNode = null;
		List<DataNode> colNodes = null;
		if(this.getSubNodes() != null && this.getSubNodes().size() > 0){
			colNodes = this.getSubNodes();
			firstColNode = colNodes.get(0);
			if(firstColNode != null && firstColNode.getSubNodes() != null){
				rowSize = firstColNode.getSubNodes().size();
			}
		}
		result += "<div>" + rowSize + "</div>";
		
		if(rowSize > 0){
			result += "<div class='TableNodeData'><table>";
			result += "<thead><tr>";
			for(int colIndex=0; colIndex < colNodes.size(); colIndex++){
				DataNode dataNode = colNodes.get(colIndex);
				result += "<td>" + dataNode.getNodeValue() + "</td>";
			}
			result += "</tr></thead>";
			
			if(firstColNode != null && firstColNode.getSubNodes() != null){
				result += "<tbody>";
				for(int rowIndex=0; rowIndex < rowSize; rowIndex++){
					result += "<tr>";
					for(int colIndex=0; colIndex < colNodes.size(); colIndex++){
						result += "<td>";
						DataNode dataNode = colNodes.get(colIndex).getSubNodes().get(rowIndex);
						result += dataNode.traveler();
						result += "</td>";
					}
					result += "</tr>";
				}
				result += "</tbody>";
			}
			result += "</table></div>";
		}
		result += "</div>";
		return result;
	}
}
